import json
from datetime import datetime
import pandas as pd
import plotly.graph_objects as go
def loadjson(fn):
f = open(fn)
buf = f.read()
f.close()
obj = json.loads(buf)
return obj
def showLine(name, x, y):
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y,
mode='lines',
name=name))
fig.show()
def funcGetX(e):
return e['x']
def showLineWithMap(name, obj, xname='', yname=''):
fig = go.Figure()
arr = []
x = []
y = []
for k in obj:
arr.append({'x': k, 'y': obj[k]})
arr.sort(key=funcGetX)
for k in range(len(arr)):
x.append(arr[k]['x'])
y.append(arr[k]['y'])
fig.add_trace(go.Scatter(x=x, y=y,
mode='lines',
name=name))
fig.update_layout(title=name)
fig.update_xaxes(title_text=xname)
fig.update_yaxes(title_text=yname)
fig.show()
report = loadjson('./simatk001.json')
# 统计不同类型的胜利次数
def analyzeUnitTypes(obj):
mapNums = {}
for i in range(len(obj['results'])):
curobj = obj['results'][i]
utWin = curobj['units'][curobj['winIndex']]['unitType']
if utWin in mapNums:
mapNums[utWin] += 1
else:
mapNums[utWin] = 1
return mapNums
analyzeUnitTypes(report)
{2: 17890, 0: 221, 1: 14650}
# 统计不同hp的战斗次数
def analyzeUnits(obj):
mapNums = {}
for i in range(len(obj['results'])):
curobj = obj['results'][i]
hp0 = curobj['units'][0]['props']['1']
hp1 = curobj['units'][1]['props']['1']
if hp0 in mapNums:
mapNums[hp0] += 1
else:
mapNums[hp0] = 1
if hp1 in mapNums:
mapNums[hp1] += 1
else:
mapNums[hp1] = 1
return mapNums
showLineWithMap('参与模拟战斗的次数', analyzeUnits(report), xname='hp (dps=200-hp)', yname='战斗次数')
# 统计不同hp的胜利次数
def analyzeUnitWinNums(obj):
mapNums = {}
for i in range(len(obj['results'])):
curobj = obj['results'][i]
hp0 = curobj['units'][0]['props']['1']
hp1 = curobj['units'][1]['props']['1']
if curobj['winIndex'] == 0:
if hp0 in mapNums:
mapNums[hp0] += 1
else:
mapNums[hp0] = 1
else:
if hp1 in mapNums:
mapNums[hp1] += 1
else:
mapNums[hp1] = 1
return mapNums
showLineWithMap('战斗胜利的次数', analyzeUnitWinNums(report), xname='hp (dps=200-hp)', yname='胜利次数')
# 统计不同hp的胜利次数,不考虑和自己打
def analyzeUnitWinNumsExpSelf(obj):
mapNums = {}
for i in range(len(obj['results'])):
curobj = obj['results'][i]
hp0 = curobj['units'][0]['props']['1']
hp1 = curobj['units'][1]['props']['1']
if hp0 == hp1:
continue
if curobj['winIndex'] == 0:
if hp0 in mapNums:
mapNums[hp0] += 1
else:
mapNums[hp0] = 1
else:
if hp1 in mapNums:
mapNums[hp1] += 1
else:
mapNums[hp1] = 1
return mapNums
showLineWithMap('不和自己战斗的胜利次数', analyzeUnitWinNumsExpSelf(report), xname='hp (dps=200-hp)', yname='胜利次数')
# 统计不同hp的胜利次数,是否先手
def analyzeUnitWinNumsFirst(obj, isfirst):
mapNums = {}
for i in range(len(obj['results'])):
curobj = obj['results'][i]
hp0 = curobj['units'][0]['props']['1']
hp1 = curobj['units'][1]['props']['1']
if curobj['winIndex'] == 0:
if (isfirst and curobj['firstIndex'] == 0) or (not isfirst and curobj['firstIndex'] == 1):
if hp0 in mapNums:
mapNums[hp0] += 1
else:
mapNums[hp0] = 1
else:
if (isfirst and curobj['firstIndex'] == 1) or (not isfirst and curobj['firstIndex'] == 0):
if hp1 in mapNums:
mapNums[hp1] += 1
else:
mapNums[hp1] = 1
return mapNums
showLineWithMap('先手战斗的胜利次数', analyzeUnitWinNumsFirst(report, True), xname='hp (dps=200-hp)', yname='胜利次数')
showLineWithMap('后手战斗的胜利次数', analyzeUnitWinNumsFirst(report, False), xname='hp (dps=200-hp)', yname='胜利次数')
# 统计不同hp战斗结束后剩余血量百分比
def analyzeUnitLastHPPer(obj):
mapNums = {}
for i in range(len(obj['results'])):
curobj = obj['results'][i]
hp0 = curobj['units'][0]['props']['1']
hp1 = curobj['units'][1]['props']['1']
lhp0 = int(curobj['units'][0]['props']['100'] * 100 / curobj['units'][0]['props']['1'])
lhp1 = int(curobj['units'][1]['props']['100'] * 100 / curobj['units'][1]['props']['1'])
if curobj['units'][0]['props']['100'] <= 0:
lhp0 = 0
if curobj['units'][1]['props']['100'] <= 0:
lhp1 = 0
if curobj['winIndex'] == 0:
if hp0 in mapNums:
mapNums[hp0] += lhp0
else:
mapNums[hp0] = lhp0
else:
if hp1 in mapNums:
mapNums[hp1] += lhp1
else:
mapNums[hp1] = lhp1
return mapNums
showLineWithMap('hp战斗后剩余血量百分比之和', analyzeUnitLastHPPer(report))
# 统计不同dps战斗结束后剩余血量百分比,总的百分比
def analyzeUnitLastHPPer2(obj):
mapNums = {}
for i in range(len(obj['results'])):
curobj = obj['results'][i]
hp0 = curobj['units'][0]['props']['1']
hp1 = curobj['units'][1]['props']['1']
# hp0 = int(curobj['units'][0]['props']['100'] * 100 / curobj['units'][0]['props']['1'])
# hp1 = int(curobj['units'][1]['props']['100'] * 100 / curobj['units'][1]['props']['1'])
# if curobj['units'][0]['props']['100'] <= 0:
# hp0 = 0
# if curobj['units'][1]['props']['100'] <= 0:
# hp1 = 0
if curobj['winIndex'] == 0:
if hp0 in mapNums:
mapNums[hp0]['cur'] += curobj['units'][0]['props']['100']
mapNums[hp0]['total'] += curobj['units'][0]['props']['1']
else:
mapNums[hp0] = {'cur': curobj['units'][0]['props']['100'], 'total': curobj['units'][0]['props']['1']}
elif curobj['winIndex'] == 1:
if hp1 in mapNums:
mapNums[hp1]['cur'] += curobj['units'][1]['props']['100']
mapNums[hp1]['total'] += curobj['units'][1]['props']['1']
else:
mapNums[hp1] = {'cur': curobj['units'][1]['props']['100'], 'total': curobj['units'][1]['props']['1']}
mapNums2 = {}
for k in mapNums:
mapNums2[k] = int(mapNums[k]['cur'] * 100 / mapNums[k]['total'])
return mapNums2
showLineWithMap('hp战斗后剩余血量百分比', analyzeUnitLastHPPer2(report))
# 统计不同dps战斗结束后剩余血量百分比,总的百分比,失败也加总数
def analyzeUnitLastHPPer3(obj):
mapNums = {}
for i in range(len(obj['results'])):
curobj = obj['results'][i]
hp0 = curobj['units'][0]['props']['1']
hp1 = curobj['units'][1]['props']['1']
# hp0 = int(curobj['units'][0]['props']['100'] * 100 / curobj['units'][0]['props']['1'])
# hp1 = int(curobj['units'][1]['props']['100'] * 100 / curobj['units'][1]['props']['1'])
# if curobj['units'][0]['props']['100'] <= 0:
# hp0 = 0
# if curobj['units'][1]['props']['100'] <= 0:
# hp1 = 0
if curobj['winIndex'] == 0:
if hp0 in mapNums:
mapNums[hp0]['cur'] += curobj['units'][0]['props']['100']
mapNums[hp0]['total'] += curobj['units'][0]['props']['1']
else:
mapNums[hp0] = {'cur': curobj['units'][0]['props']['100'], 'total': curobj['units'][0]['props']['1']}
if hp1 in mapNums:
mapNums[hp1]['total'] += curobj['units'][1]['props']['1']
else:
mapNums[hp1] = {'cur': 0, 'total': curobj['units'][1]['props']['1']}
elif curobj['winIndex'] == 1:
if hp1 in mapNums:
mapNums[hp1]['cur'] += curobj['units'][1]['props']['100']
mapNums[hp1]['total'] += curobj['units'][1]['props']['1']
else:
mapNums[hp1] = {'cur': curobj['units'][1]['props']['100'], 'total': curobj['units'][1]['props']['1']}
if hp0 in mapNums:
mapNums[hp0]['total'] += curobj['units'][0]['props']['1']
else:
mapNums[hp0] = {'cur': 0, 'total': curobj['units'][0]['props']['1']}
mapNums2 = {}
for k in mapNums:
mapNums2[k] = mapNums[k]['cur'] / mapNums[k]['total']
return mapNums2
showLineWithMap('剩余HP百分比', analyzeUnitLastHPPer3(report), xname='hp (dps=200-hp)', yname='剩余HP (百分比)')
# 统计不同hp战斗结束后剩余血量
def analyzeUnitLastHP(obj):
mapNums = {}
for i in range(len(obj['results'])):
curobj = obj['results'][i]
hp0 = curobj['units'][0]['props']['1']
hp1 = curobj['units'][1]['props']['1']
lhp0 = curobj['units'][0]['props']['100']
lhp1 = curobj['units'][1]['props']['100']
# if curobj['units'][0]['props']['100'] <= 0:
# hp0 = 0
# if curobj['units'][1]['props']['100'] <= 0:
# hp1 = 0
if curobj['winIndex'] == 0:
if hp0 in mapNums:
mapNums[hp0] += lhp0
else:
mapNums[hp0] = lhp0
else:
if hp1 in mapNums:
mapNums[hp1] += lhp1
else:
mapNums[hp1] = lhp1
return mapNums
showLineWithMap('战斗后剩余HP', analyzeUnitLastHP(report), xname='hp (dps=200-hp)', yname='剩余HP')